﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;

namespace sdd.subsystems.ui.screens
{
    class ScrNetworkSetup : IScreen
    {
        private OptionsMenuItem<int> optEthernetMode;

        public string ScreenName
        {
            get { return "Network Setup"; }
        }

        public display.IDisplay Display {get; set;}

        public apps.IApplication Application { get; set;}

        public ScreenManager Manager { set; get; }

        public void Initialize()
        {
            RegisterMenuItems();
        }

        public void Show()
        {
            //
        }

        public void Hide()
        {
            //
        }

        public void ProcessKeyPadEvent(display.KeypadEventArgs keyPadEventArgs)
        {
            //
        }



        private void RegisterMenuItems()
        {
            MenuItem mnuNetwork = new MenuItem("sdd.menu.network_settings", "Network");
            Manager.Menus.Register(mnuNetwork, Manager.Director.SystemMenu);

            // ethernet mode setting

            List<OptionsMenuItem<int>.ListEntry<int>> ethernetModes = new List<OptionsMenuItem<int>.ListEntry<int>>();
            ethernetModes.Add(new OptionsMenuItem<int>.ListEntry<int>("Disabled", 0));
            ethernetModes.Add(new OptionsMenuItem<int>.ListEntry<int>("DHCP", 1));
            ethernetModes.Add(new OptionsMenuItem<int>.ListEntry<int>("Static IP", 2));

            optEthernetMode = new OptionsMenuItem<int>("sdd.settings.network.ethernet.mode", "Ethernet Mode", ethernetModes, 2);
            optEthernetMode.ItemClick += OptEthernetMode_ItemClick;
            Manager.Menus.Register(optEthernetMode, mnuNetwork);

            // ethernet IP (TODO: read only atm)

            Manager.Menus.Register(new IPAddressMenuItem("sdd.menu.network_settings.ethernet.ip", "Ethernet IP", LocalIPAddress()), mnuNetwork);
        }

        private void OptEthernetMode_ItemClick(object sender, MenuItemClickEventArgs<int> e)
        {
            throw new NotImplementedException();
        }



        private IPAddress GetSubnetMask(IPAddress address)
        {
            foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
            {
                foreach (UnicastIPAddressInformation unicastIPAddressInformation in adapter.GetIPProperties().UnicastAddresses)
                {
                    if (unicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        if (address.Equals(unicastIPAddressInformation.Address))
                        {
                            return unicastIPAddressInformation.IPv4Mask;
                        }
                    }
                }
            }
            throw new ArgumentException(string.Format("Can't find subnetmask for IP address '{0}'", address));
        }

        private IPAddress LocalIPAddress()
        {
            IPHostEntry host;
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    return ip;
                }
            }
            return null;
        }
    }
}
